
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <title>浏览页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body class="pear-container">
    
        <!-- 顶部查询表单 -->
        <!-- 顶部查询表单 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <form class="layui-form top-search-from">


                    <div class="layui-form-item">
                        <label class="layui-form-label">下单时间</label>
                        <div class="layui-input-block">
                            <div class="layui-input-block" id="order_date_time">
                                <input type="text" autocomplete="off" name="orderDateTime[]" id="order_date_time-date-start" class="layui-input inline-block" placeholder="开始时间">
                                -
                                <input type="text" autocomplete="off" name="orderDateTime[]" id="order_date_time-date-end" class="layui-input inline-block" placeholder="结束时间">
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">会员号</label>
                        <div class="layui-input-block">
                            <input type="text" name="customerNumber" value="" class="layui-input" placeholder="请输入会员号">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">订单号</label>
                        <div class="layui-input-block">
                            <input type="text" name="orderNo" value="" class="layui-input" placeholder="请输入订单号">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">联系人电话</label>
                        <div class="layui-input-block">
                            <input type="text" name="contactTel" value="" class="layui-input" placeholder="请输入手机号">
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-block">
                            <select name="is_reward">
                                <option value=""></option>
                                <option value="0">未发放奖励</option>
                                <option value="1">已发放奖励</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-form-item layui-inline">
                        <label class="layui-form-label"></label>
                        <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="table-query">
                            <i class="layui-icon layui-icon-search"></i>查询
                        </button>
                        <button type="reset" class="pear-btn pear-btn-md" lay-submit lay-filter="table-reset">
                            <i class="layui-icon layui-icon-refresh"></i>重置
                        </button>
                    </div>
                    <div class="toggle-btn">
                        <a class="layui-hide">展开<i class="layui-icon layui-icon-down"></i></a>
                        <a class="layui-hide">收起<i class="layui-icon layui-icon-up"></i></a>
                    </div>
                </form>
            </div>
        </div>
        
        <!-- 数据表格 -->
        <div class="layui-card">
            <div class="layui-card-body">
                <table id="data-table" lay-filter="data-table"></table>
            </div>
        </div>

        <script type="text/html" id="pay_method_text">
            <span class="layui-badge-rim">{{ d.pay_method_text }}</span>
        </script>
        <script type="text/html" id="is_reward">
            {{#if (d.is_reward == 1) { }}
            <span class="layui-badge layui-bg-blue">已发放</span>
            {{# }else{ }}
            <span class="layui-badge layui-bg-orange">未发放</span>
            {{# } }}
        </script>
        <script type="text/html" id="items">
            <span class="layui-badge layui-bg-green tool-btn" lay-event="itemsInfo">点击查看</span>
        </script>



        <!-- 表格顶部工具栏 -->
        <script type="text/html" id="table-toolbar">
            <button class="pear-btn pear-btn-success pear-btn-md" disabled>
                资金池金额：<span id="total_amount">0</span>
            </button>
        </script>

        <!-- 表格行工具栏 -->
<!--        <script type="text/html" id="table-bar">-->
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="edit" permission="app.admin.networkorder.update">编辑</button>-->
<!--            <button class="pear-btn pear-btn-xs tool-btn" lay-event="remove" permission="app.admin.networkorder.delete">删除</button>-->
<!--        </script>-->

        <script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script src="/app/admin/admin/js/common.js"></script>
        
        <script>

            // 相关常量
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/network-order/select";
            const UPDATE_API = "/app/admin/network-order/update";
            const DELETE_API = "/app/admin/network-order/delete";
            const INSERT_URL = "/app/admin/network-order/insert";
            const UPDATE_URL = "/app/admin/network-order/update";
            const ITEMS_URL = "/app/admin/network-order/itemsInfo";
            
            // 表格渲染
            layui.use(["table", "form", "common", "popup", "util"], function() {
                let table = layui.table;
                let form = layui.form;
                let $ = layui.$;
                let common = layui.common;
                let util = layui.util;
                
				// 表头参数
				let cols = [
                    {
                        title: "订单编号",align: "center",
                        field: "orderNo",
                    },{
						title: "联系人电话",align: "center",
						field: "contactTel",
					},{
                        title: "总金额",align: "center",
                        field: "totalAmount",
                    },{
                        title: "下单时间",align: "center",
                        field: "orderDateTime",
                    },{
                        title: "支付方式",align: "center",
                        field: "pay_method_text",
                        templet: "#pay_method_text"
                    },{
                        title: "购买商品",align: "center",
                        templet: "#items"
                    },{
                        title: "返还金额",align: "center",
                        field: "reward_total_price",
                    },{
                        title: "进池金额",align: "center",
                        field: "rake_price",
                    },{
                        title: "奖励",align: "center",
                        field: "is_reward",
                        templet: "#is_reward"
                    },{
                        title: "进度",align: "center",
                        field: "progress",
                    },{
                        title: "奖励发放时间",align: "center",
                        field: "reward_create_time",
                    }
				];
				
				// 渲染表格
				table.render({
				    elem: "#data-table",
				    url: SELECT_API,
				    page: true,
				    cols: [cols],
				    skin: "line",
				    size: "lg",
				    toolbar: "#table-toolbar",
				    autoSort: false,
                    limit: 20,
				    defaultToolbar: [{
				        title: "刷新",
				        layEvent: "refresh",
				        icon: "layui-icon-refresh",
				    }, "filter", "print", "exports"],
				    done: function (res) {
                        $("#total_amount").text(res.customize.total_price);
				        layer.photos({photos: 'div[lay-id="data-table"]', anim: 5});
				    }
				});
				
				
                // 编辑或删除行事件
                table.on("tool(data-table)", function(obj) {
                    if (obj.event === "remove") {
                        remove(obj);
                    } else if (obj.event === "edit") {
                        edit(obj);
                    }
                });

                // 表格顶部工具栏事件
                table.on("toolbar(data-table)", function(obj) {
                    if (obj.event === "add") {
                        add();
                    } else if (obj.event === "refresh") {
                        refreshTable();
                    } else if (obj.event === "batchRemove") {
                        batchRemove(obj);
                    }
                });

                // 表格顶部搜索事件
                form.on("submit(table-query)", function(data) {
                    table.reload("data-table", {
                        page: {
                            curr: 1
                        },
                        where: data.field
                    })
                    return false;
                });
                
                // 表格顶部搜索重置事件
                form.on("submit(table-reset)", function(data) {
                    table.reload("data-table", {
                        where: []
                    })
                });
                
                // 字段允许为空
                form.verify({
                    phone: [/(^$)|^1\d{10}$/, "请输入正确的手机号"],
                    email: [/(^$)|^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/, "邮箱格式不正确"],
                    url: [/(^$)|(^#)|(^http(s*):\/\/[^\s]+\.[^\s]+)/, "链接格式不正确"],
                    number: [/(^$)|^\d+$/,'只能填写数字'],
                    date: [/(^$)|^(\d{4})[-\/](\d{1}|0\d{1}|1[0-2])([-\/](\d{1}|0\d{1}|[1-2][0-9]|3[0-1]))*$/, "日期格式不正确"],
                    identity: [/(^$)|(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
                });

                // 表格排序事件
                table.on("sort(data-table)", function(obj){
                    table.reload("data-table", {
                        initSort: obj,
                        scrollPos: "fixed",
                        where: {
                            field: obj.field,
                            order: obj.type
                        }
                    });
                });

                // 表格新增数据
                let add = function() {
                    layer.open({
                        type: 2,
                        title: "新增",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: INSERT_URL
                    });
                }

                // 表格编辑数据
                let edit = function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "修改",
                        shade: 0.1,
                        maxmin: true,
                        area: [common.isModile()?"100%":"500px", common.isModile()?"100%":"450px"],
                        content: UPDATE_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                }

                table.on("tool(data-table)", function(obj) {
                    let value = obj.data[PRIMARY_KEY];
                    layer.open({
                        type: 2,
                        title: "购买商品数据",
                        shade: 0.1,
                        maxmin: true,
                        area: ['700px', '800px'],
                        content: ITEMS_URL + "?" + PRIMARY_KEY + "=" + value
                    });
                });

                // 删除一行
                let remove = function(obj) {
                    return doRemove(obj.data[PRIMARY_KEY]);
                }

                // 删除多行
                let batchRemove = function(obj) {
                    let checkIds = common.checkField(obj, PRIMARY_KEY);
                    if (checkIds === "") {
                        layui.popup.warning("未选中数据");
                        return false;
                    }
                    doRemove(checkIds.split(","));
                }

                // 执行删除
                let doRemove = function (ids) {
                    let data = {};
                    data[PRIMARY_KEY] = ids;
                    layer.confirm("确定删除?", {
                        icon: 3,
                        title: "提示"
                    }, function(index) {
                        layer.close(index);
                        let loading = layer.load();
                        $.ajax({
                            url: DELETE_API,
                            data: data,
                            dataType: "json",
                            type: "post",
                            success: function(res) {
                                layer.close(loading);
                                if (res.code) {
                                    return layui.popup.failure(res.msg);
                                }
                                return layui.popup.success("操作成功", refreshTable);
                            }
                        })
                    });
                }

                // 刷新表格数据
                window.refreshTable = function() {
                    table.reloadData("data-table", {
                        scrollPos: "fixed",
                        done: function (res, curr) {
                            if (curr > 1 && res.data && !res.data.length) {
                                curr = curr - 1;
                                table.reloadData("data-table", {
                                    page: {
                                        curr: curr
                                    },
                                })
                            }
                        }
                    });
                }
            })

        </script>
    </body>
</html>
